package com.group.itms.routes.activities;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.group.itms.routes.R;
import com.group.itms.routes.adapters.MultiViewTypeAdapter;
import com.group.itms.routes.data.BObject;
import com.group.itms.routes.data.ObjectType;
import com.group.itms.routes.utils.Header;
import com.group.itms.routes.utils.Item;

public class InformationPointListActivity extends BaseActivity {

	private BObject mObject;
	
	public static Intent getIntent(Context c, int objId) {
		Intent intent = new Intent(c, InformationPointListActivity.class);
		intent.putExtra(OBJECT_ID, objId);
		return intent;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_inf_point_list);
		initObject();
		((TextView)findViewById(R.id.title)).setText(mObject.getName());
		List<BObject> infoPoints;
		if (mObject.is(ObjectType.ROUTE)) {
			infoPoints = mObject.getChildrenPoints();
		} else {
			infoPoints = new ArrayList<BObject>();
			List<BObject> routes = mObject.getChildrenRoutes();
			for (BObject route : routes) {
				infoPoints.addAll(route.getChildrenPoints());
			}
		}
		final MultiViewTypeAdapter adapter = new MultiViewTypeAdapter(this);
		adapter.add(new Header("Information Points"));
		adapter.addAll(toItems(infoPoints));
		ListView pointsListView = (ListView) findViewById(R.id.inf_points_list);
		pointsListView.setAdapter(adapter);
		pointsListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				BObject point = (BObject) adapter.getItemData(position);
				Context ctx = InformationPointListActivity.this;
				Intent i = InformationPointActivity.getIntent(ctx, point.getId());
				startActivity(i, true);
			}
		});
		setTitle("Information Points");
	}

	private void initObject() {
		// XXX add validations?
		Bundle args = getIntent().getExtras();
		int routeId = args.getInt(OBJECT_ID);
		mObject = getObject(routeId);
	}

	private List<Item> toItems(List<BObject> infoPoints) {
		List<Item> items = new ArrayList<Item>();
		for (BObject point : infoPoints) {
			items.add(new Item(point.getIndName(), point));
		}
		return items;
	}
	
	@Override
	protected String getTag() {
		return "InformationPointListActivity";
	}

}
